using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using System.Globalization;

namespace WebControls
{
    public class SFTextBox : TextBox, IClear, IValidation, ILabel, IReadOnly
    {

        #region IClear Members

        public void Clear()
        {
            this.Text = "";
        }

        #endregion

        #region IValidation Members

        private bool _AllowNull;
        private bool _HasErrors;
        private string _ErrorMessage;

        public bool AllowNull
        {
            get
            {
                return _AllowNull;
            }
            set
            {
                _AllowNull = value;
            }
        }

        public bool HasErrors
        {
            get
            {
                return _HasErrors;
            }

        }

        public string ErrorMessage
        {
            get
            {
                return _ErrorMessage;
            }
            set
            {
                _HasErrors = (value != "");
                _ErrorMessage = value;
            }
        }

        public virtual bool Validate()
        {
            if (!_AllowNull && this.Visible)
            {
                if (!Validation.ValidateNotNull(this.Text))
                {
                    this.ErrorMessage += StaticMethods.GetErrorMessageMultilanguageString(1,new object[]{ StaticMethods.GetErrorMessageControlName(this) });
                }
            }
            else
            {
                if (this.Text.Length > 0 && (this.Text.Length > this.MaxLength || this.Text.Trim().Length < _MinLength) && this.Visible && MaxLength != MinLength)
                {
                    this.ErrorMessage += StaticMethods.GetErrorMessageMultilanguageString(12, new object[] { StaticMethods.GetErrorMessageControlName(this), _MinLength.ToString(), MaxLength.ToString()  });
                }
            }
            return this.HasErrors;
        }

        #endregion


        #region ILabel Members

        private string _ControlsLabel;
        [DefaultValueAttribute("")]
        [IDReferencePropertyAttribute]
        [TypeConverterAttribute(typeof(ControlIDConverter))]
        public string ControlsLabel
        {
            get
            {
                return _ControlsLabel;
            }
            set
            {
                _ControlsLabel = value;
            }
        }

        #endregion


        private short _MinLength = 0;
        public short MinLength
        {
            get
            {
                return _MinLength;
            }
            set
            {
                _MinLength = value;
            }
        }

        #region IReadOnly Members

        bool IReadOnly.ReadOnly
        {
            get
            {
                return !this.Enabled;
            }
            set
            {
                this.Enabled = !value;
            }
        }

        #endregion

        private bool _AutoTrim = true;
       
        private TextStyles _TextStyle = TextStyles.Free;
        private bool _FormatNumberOnClient = true;


      
        public bool AutoTrim
        {
            get { return _AutoTrim; }
            set { _AutoTrim = value; }
        }

     
        public TextStyles TextStyle
        {
            get 
            { 
                return _TextStyle; 
            }
            set
            {
                _TextStyle = value;
                switch (TextStyle)
                {
                    case TextStyles.AllUpper:
                        this.Style.Add("text-transform", "uppercase");
                        break;
                    case TextStyles.AllLower:
                        this.Style.Add("text-transform", "lowercase");
                        break;
                    case TextStyles.FirstUpper:
                        this.Style.Add("text-transform", "capitalize");
                        break;
                    case TextStyles.Free:
                        this.Style.Remove("text-transform");
                        break;
                }
            }
        }

       
        public string Value
        {
            get
            {
                string txt = "";
                if (AutoTrim)
                {
                    txt = this.Text.Trim();
                }
                else
                {
                    txt = this.Text;
                }
                if (txt.Length > 0)
                {
                    switch (TextStyle)
                    {
                        case TextStyles.AllUpper:
                            return txt.ToUpper(new CultureInfo("tr-TR"));
                        case TextStyles.AllLower:
                            return txt.ToLower(new CultureInfo("tr-TR"));
                        case TextStyles.FirstUpper:
                            return txt.Substring(0, 1).ToUpper(new CultureInfo("tr-TR")) + txt.Substring(1).ToLower(new CultureInfo("tr-TR"));
                        case TextStyles.Free:
                            return txt;
                    }
                    return txt;
                }
                else
                {
                    return txt;
                }

            }
            set
            {
                string txt = "";
                if (value != null)
                {
                    if (AutoTrim)
                    {
                        txt = value.Trim();
                    }
                    else
                    {
                        txt = value;
                    }
                }
                if (txt.Length > 0)
                {
                    switch (TextStyle)
                    {
                        case TextStyles.AllUpper:
                            this.Text = txt.ToUpper(new CultureInfo("tr-TR"));
                            break;
                        case TextStyles.AllLower:
                            this.Text = txt.ToLower(new CultureInfo("tr-TR"));
                            break;
                        case TextStyles.FirstUpper:
                            this.Text = txt.Substring(0, 1).ToUpper(new CultureInfo("tr-TR")) + txt.Substring(1).ToLower(new CultureInfo("tr-TR"));
                            break;
                        case TextStyles.Free:
                            this.Text = txt;
                            break;
                    }
                    this.Text = txt;
                }
                else
                {
                    this.Text = txt;
                }
            }
        }

        
        public bool FormatNumberOnClient
        {
            get { return _FormatNumberOnClient; }
            set { _FormatNumberOnClient = value; }
        }

        public enum TextStyles
        {
            FirstUpper,
            AllUpper,
            AllLower,
            Free
        }

        protected override void OnLoad(EventArgs e)
        {
            List<SFLabel> lblRetval = StaticMethods.GetLabel(this);
            if (lblRetval != null)
            {
                foreach (SFLabel lbl in lblRetval)
                {
                    if (this.AllowNull)
                    {
                        lbl.CssClass = "cssLabelNormal";
                    }
                    else
                    {
                        lbl.CssClass = "cssLabelRequired";
                    }
                }
            }
            this.CssClass = "controlNormal";
            this.Attributes.Add("onfocus", "controlFocus(this)");
            this.Attributes.Add("onmouseout", "controlOnMouseOut(this)");
            this.Attributes.Add("onmouseover", "controlOnMouseOver(this)");
            this.Attributes.Add("onblur", "controlOnBlur(this)");
            this.AutoCompleteType = AutoCompleteType.Disabled;

            base.OnLoad(e);
        }
    }
}
